"""
GT-LMS System Validation Tools
This module provides various validation functions used in the system
"""

import re
from datetime import datetime
from typing import Optional


def validate_email(email: str) -> bool:
    """
    Validate email format
    
    Args:
        email (str): Email address to validate
        
    Returns:
        bool: True if email is valid, False otherwise
    """
    pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    return re.match(pattern, email) is not None


def validate_password_strength(password: str) -> bool:
    """
    Validate password strength
    Password must be at least 8 characters long and contain:
    - At least one uppercase letter
    - At least one lowercase letter
    - At least one digit
    - At least one special character
    
    Args:
        password (str): Password to validate
        
    Returns:
        bool: True if password is strong enough, False otherwise
    """
    if len(password) < 8:
        return False

    # Check uppercase letters
    if not re.search(r'[A-Z]', password):
        return False

    # Check lowercase letters
    if not re.search(r'[a-z]', password):
        return False

    # Check digits
    if not re.search(r'\d', password):
        return False

    # Check special characters
    if not re.search(r'[!@#$%^&*(),.?":{}|<>]', password):
        return False

    return True


def validate_date_format(date_string: str, date_format: str = "%Y-%m-%d") -> bool:
    """
    Validate date string format

    Args:
        date_string (str): Date string to validate
        date_format (str): Expected date format (default: YYYY-MM-DD)

    Returns:
        bool: True if date format is valid, False otherwise
    """
    try:
        datetime.strptime(date_string, date_format)
        return True
    except ValueError:
        return False


def validate_student_id(student_id: str) -> bool:
    """
    Validate student ID format
    Student ID should be alphanumeric and 6-12 characters long

    Args:
        student_id (str): Student ID to validate

    Returns:
        bool: True if student ID is valid, False otherwise
    """
    if not isinstance(student_id, str):
        return False

    # Check length
    if len(student_id) < 6 or len(student_id) > 12:
        return False

    # Check if alphanumeric
    return student_id.isalnum()


def validate_course_id(course_id: str) -> bool:
    """
    Validate course ID format
    Course ID should be alphanumeric and allow hyphens, 4-10 characters long

    Args:
        course_id (str): Course ID to validate

    Returns:
        bool: True if course ID is valid, False otherwise
    """
    if not isinstance(course_id, str):
        return False

    # Check length
    if len(course_id) < 4 or len(course_id) > 10:
        return False

    # Check if alphanumeric or contains hyphens
    pattern = r'^[a-zA-Z0-9-]+$'
    return re.match(pattern, course_id) is not None


def validate_grade(score: float, max_score: float) -> bool:
    """
    Validate grade score

    Args:
        score (float): Score to validate
        max_score (float): Maximum possible score

    Returns:
        bool: True if grade is valid, False otherwise
    """
    if not isinstance(score, (int, float)) or not isinstance(max_score, (int, float)):
        return False

    if max_score <= 0:
        return False

    return 0 <= score <= max_score